//
//  clsFileFun.m
//  AntsExpress
//
//  Created by slccper on 16/1/1.
//  Copyright © 2016年 slccper. All rights reserved.
//
#import "clsFileFun.h"

@implementation clsFileFun


+ (NSString *)getUsersPhotoDir
{
    NSString *DocumentFolder = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
	NSString *path = [NSString stringWithFormat:@"%@/Users",DocumentFolder];
	return path;
}
+ (NSString *)getBannerPhotoDir
{
    NSString *DocumentFolder = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *path = [NSString stringWithFormat:@"%@/banner",DocumentFolder];
    return path;
}
+ (NSString *)getUsersPhotoDir:(NSString *)Pic{
    NSString *DocumentFolder = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
	NSString *path = [NSString stringWithFormat:@"%@/Users/%@", DocumentFolder,Pic];
	return path;
}
+ (NSString *)getShowImage{
    NSString *DocumentFolder = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *path = [NSString stringWithFormat:@"%@/Show", DocumentFolder];
    return path;
}
+ (NSString *)getFilePath:(NSString *)pDir :(NSString *)pFileName {
    return [NSString stringWithFormat:@"%@/%@", pDir, pFileName];
}

+ (NSString *)getPath:(NSString *)pFolder filename:(NSString *)pfilename {
    NSString *DocumentFolder = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
	NSString *path = [NSString stringWithFormat:@"%@/%@/%@",DocumentFolder, pFolder, pfilename];
	return path;
}

+ (BOOL)saveFile:(NSString*)path :(NSData *)data {
    BOOL blnreturn=[data writeToFile:path atomically:YES];
    [clsFileFun addSkipBackupAttributeAtPath:path];
	return blnreturn;
}

+ (NSData *)loadFile:(NSString*)path {
	if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
		return [NSData dataWithContentsOfFile:path];
	}
	return nil;
}

+ (BOOL)addSkipBackupAttributeAtPath:(NSString *)pPath {
    const char* filePath = [pPath fileSystemRepresentation];
    const char* attrName = "com.apple.MobileBackup";
    if (&NSURLIsExcludedFromBackupKey == nil) {
        // iOS 5.0.1 and lower
        u_int8_t attrValue = 1;
        int result = setxattr(filePath, attrName, &attrValue, sizeof(attrValue), 0, 0);
        return result == 0;
    } else {
        // First try and remove the extended attribute if it is present
        int result = getxattr(filePath, attrName, NULL, sizeof(u_int8_t), 0, 0);
        if (result != -1) {
            // The attribute exists, we need to remove it
            int removeResult = removexattr(filePath, attrName, 0);
            if (removeResult == 0) {
                NSLog(@"Removed extended attribute on file %@", pPath);
            }
        }
        
        // Set the new key
        return [[NSURL fileURLWithPath:pPath] setResourceValue:[NSNumber numberWithBool:YES] forKey:NSURLIsExcludedFromBackupKey error:nil];
    }
}


@end
